package com.example.mydemoproject.task;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;

/**
 * @author LiuZhiYong
 * @description
 * @date 2022-09-29
 **/
public class Test {

    public static void main(String[] args) throws InterruptedException {
        int processors = Runtime.getRuntime().availableProcessors();
        System.out.println("CPU cores: " + processors);
//        test1();
        test2();
    }

    public static void test1() throws InterruptedException {
        long start = System.currentTimeMillis();
        List list = new ArrayList();
        int size = 100;
        for (int i = 0; i < size; i++) {
            list.add(i);
        }
        for (int j = 0; j < list.size(); j++) {
            System.out.println(j);
            Thread.sleep(500);
        }
        long end = System.currentTimeMillis();
        System.out.println(start);
        System.out.println(end);
        System.out.println("Test总时间 = " + (end - start) + "ms");
    }

    public static void test2() {
        long start = System.currentTimeMillis();

        List list = new ArrayList();
        int size = 100;
        for (int i = 0; i < size; i++) {
            list.add(i);
        }

        Task task = new Task(0, size-1, list);
        ForkJoinPool forkJoinPool = null;
        try{
            forkJoinPool = new ForkJoinPool();
            Boolean invoke = forkJoinPool.invoke(task);
            System.out.println("结果: "+ invoke);
        } finally {
            if (forkJoinPool != null) {
                forkJoinPool.shutdown();
            }
        }

        long end = System.currentTimeMillis();
        System.out.println(start);
        System.out.println(end);
        System.out.println("Test总时间 = " + (end - start) + "ms");
    }

}